// pages/openbillbox/openbill/openbill.js
import {
  orderSubmit
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sellerInfo: null,
    goodsList: [],
    isDaiShou: false,
    shouAddressInfo: null,
    faAddressInfo: null,
    totalRemark: "",
    daiShouMoney: "",
    dingJin: "",
    addProduct: false,
    disPage: true,
    subing: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let sellerInfo = JSON.parse(options.sellerInfo);
    let goodsInfo = JSON.parse(unescape(options.goodsInfo));
    let shouAddressInfo = JSON.parse(options.shouAddressInfo)
    this.data.goodsList.push(goodsInfo);
    let faInfo = null;
    if (!this.data.faAddressInfo) {

      let sender_info = wx.getStorageSync('sender_info')
      if (sender_info) {
        faInfo = {};
        sender_info = JSON.parse(sender_info);
        faInfo.name = sender_info.name;
        faInfo.phone = sender_info.mobile
      }
    }
    this.setData({
      sellerInfo: sellerInfo,
      goodsList: this.data.goodsList,
      faAddressInfo: this.data.faAddressInfo ? this.data.faAddressInfo : faInfo,
      shouAddressInfo: shouAddressInfo ? shouAddressInfo : this.data.shouAddressInfo
    })
    if (options.addProduct == "true") {
      wx.navigateTo({
        url: '../addgoods/addgoods?item=' + JSON.stringify(this.data.sellerInfo) + '&isAdd=true',
      })
      this.setData({
        addProduct: options.addProduct
      })
    } else {
      this.setData({
        addProduct: options.addProduct
      })
    }

  },
  onShow: function() {
    let self = this;
    if (this.data.addProduct == "true") {
      this.setData({
        disPage: true
      })
      setTimeout(function() {
        self.setData({
          disPage: false
        })
      }, 1000)
    } else {
      this.setData({
        disPage: false
      })
    }

  },
  goSelectSeller: function() {
    if(!this.data.sellerInfo.akai){
      wx.navigateTo({
        url: '../selectseller/selectseller?openType=xiugai',
      })
    }
   
  },
  goAddGoods: function() {
    wx.navigateTo({
      url: '../addgoods/addgoods?item=' + escape(JSON.stringify(this.data.sellerInfo)) + '&isAdd=true',
    })
  },
  changeItem: function(e) {
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../addgoods/addgoods?item=' + escape(JSON.stringify(this.data.sellerInfo)) + '&isAdd=true&changeGoodsInfo=' + escape(JSON.stringify(item)) +"&changeGoodsIndex="+index,
    })
  },
  //是否为代收货款
  changeDaiShou: function(e) {
    this.setData({
      isDaiShou: e.detail.value,
    })
  },
  //获取代收金额
  getDaiShouMoney: function(e) {
    this.setData({
      daiShouMoney: e.detail.value
    })
  },
  //获取定金
  getDingJin: function(e) {
    this.setData({
      dingJin: e.detail.value
    })
  },

  //跳转到发件人地址
  editSendAddress: function() {
    let {
      faAddressInfo
    } = this.data
    wx.navigateTo({
      url: '../sendaddress/sendaddress?faAddressInfo=' + (faAddressInfo ? JSON.stringify(faAddressInfo) : ""),
    })
  },
  //跳转到收件人地址
  editReceiveAddress: function() {
    let {
      shouAddressInfo
    } = this.data
    wx.navigateTo({
      url: '../receiveaddress/receiveaddress?shouAddressInfo=' + (shouAddressInfo ? JSON.stringify(shouAddressInfo) : ""),
    })

  },
  editTotalReamrk: function() {
    let {
      totalRemark
    } = this.data
    wx.navigateTo({
      url: '../totalremark/totalremark?totalRemark=' + totalRemark,
    })
  },
  /**
   * 开单提交订单
   * supplier_id,  //供应商id
   * p_c_b //收件人省市区
   * address,//收件人详细地址
   * name,//收件人名称
   * mobile,//收件人手机
   * earnest,//定金
   * sender_name,//发件人名称
   * sender_mobile,//发件人姓名
   * money_on_delivery,//代收货款
   * pay_state,//支付状态（1 支付，0 未支付）
   * items,//商品列表
   * is_cod,//是否为代收货款
   */
  orderSubmit: function(e) {
let self = this;
    if (!this.data.subing) {
      this.setData({
        subing: true
      })
      let {
        sellerInfo,
        goodsList,
        isDaiShou,
        shouAddressInfo,
        faAddressInfo,
        dingJin,
        daiShouMoney,
        totalRemark
      } = this.data;
      if (!sellerInfo) {
        errorMsg("请选择供应商");
      } else if (!shouAddressInfo) {
        errorMsg("请填写收货人地址");
      } else if (shouAddressInfo.name == "") {
        errorMsg("请填写收货人姓名");
      } else if (shouAddressInfo.phone == "") {
        errorMsg("请填写收货人电话");
      } else {
        orderSubmit(sellerInfo.id, shouAddressInfo.shenShiQu, shouAddressInfo.address, shouAddressInfo.name, shouAddressInfo.phone, dingJin, faAddressInfo ? faAddressInfo.name : "", faAddressInfo ? faAddressInfo.phone : "", daiShouMoney, goodsList, isDaiShou, totalRemark, e.detail.formId, (r) => {
          if (r.code == 0) {
            wx.redirectTo({
              url: '../../myorderbox/sendsuccess/sendsuccess?dataInfo=' +escape(JSON.stringify(r.data)),
            })
          } else {
            errorMsg(r.message)
            self.setData({
              subing: false
            })
          }
        })
      }
    }

  }
})